package cn.seocoo.platform.plate.service.impl;

import cn.seocoo.platform.base.dto.BaseCommonDto;
import cn.seocoo.platform.base.dto.BaseCommonParentsDto;
import cn.seocoo.platform.common.core.text.Convert;
import cn.seocoo.platform.common.utils.StringUtils;
import cn.seocoo.platform.plate.domain.PlateHome;
import cn.seocoo.platform.plate.mapper.PlateHomeMapper;
import cn.seocoo.platform.plate.service.IPlateHomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

/**
 * 车牌归属地 服务层实现
 *
 * @author wujian
 * @date 2019-12-24
 */
@Service
public class PlateHomeServiceImpl implements IPlateHomeService {
    @Autowired
    private PlateHomeMapper plateHomeMapper;

    /**
     * 查询车牌归属地信息
     *
     * @param id 车牌归属地ID
     * @return 车牌归属地信息
     */
    @Override
    public PlateHome selectPlateHomeById(Integer id) {
        return plateHomeMapper.selectPlateHomeById(id);
    }

    /**
     * 查询车牌归属地列表
     *
     * @param plateHome 车牌归属地信息
     * @return 车牌归属地集合
     */
    @Override
    public List<PlateHome> selectPlateHomeList(PlateHome plateHome) {
        return plateHomeMapper.selectPlateHomeList(plateHome);
    }

    /**
     * 新增车牌归属地
     *
     * @param plateHome 车牌归属地信息
     * @return 结果
     */
    @Override
    public int insertPlateHome(PlateHome plateHome) {
        return plateHomeMapper.insertPlateHome(plateHome);
    }

    /**
     * 修改车牌归属地
     *
     * @param plateHome 车牌归属地信息
     * @return 结果
     */
    @Override
    public int updatePlateHome(PlateHome plateHome) {
        return plateHomeMapper.updatePlateHome(plateHome);
    }

    /**
     * 删除车牌归属地对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deletePlateHomeByIds(String ids) {
        return plateHomeMapper.deletePlateHomeByIds(Convert.toStrArray(ids));
    }

    /**
     * @Author WuJian
     * @Description //查询车牌的归属地
     * @Date 17:35 2019/12/24
     * @Param []
     * @return java.util.List<cn.seocoo.platform.base.dto.BaseCommonParentsDto>
     **/
    @Override
    public List<BaseCommonParentsDto> getPlateHomeList() {
        List<BaseCommonParentsDto> plateHomesList = plateHomeMapper.selectPlateHomeGroupName();
        if (StringUtils.isNotEmpty(plateHomesList)) {
            Iterator<BaseCommonParentsDto> it = plateHomesList.iterator();
            while (it.hasNext()) {
                BaseCommonParentsDto dto = it.next();
                List<BaseCommonDto> dtoList = plateHomeMapper.selectPlateHomeByCodeName(dto.getText());
                if (dtoList != null && dtoList.size() > 0) {
                    dto.setChildrenList(dtoList);
                } else {
                    it.remove();
                }
            }
        }

        return plateHomesList;
    }


}
